﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.FMCG.Buz.Interfaces;
using Microhard.FMCG.Buz.Models;
using HtmlAgilityPack;

namespace Microhard.FMCG.Buz.Impls.KASystem.WUMart
{
    public class OrderWithStoreParser : IHtmlParser<List<OrderModel>>
    {
        public List<OrderModel> ParseObject(string html)
        {
            List<OrderModel> result = new List<OrderModel>();
            if (string.IsNullOrEmpty(html))
                return result;

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            //获得数据table
            var table = doc.GetElementbyId("MyDataGrid");
            if (table != null)
            {
                //获得tr
                var rows = table.Descendants("tr").Where(tr => tr.Attributes["align"].Value == "right").ToList();
                foreach (var tr in rows)
                {
                    if (tr.ChildNodes.Count == 13)
                    {
                        OrderModel newOrder = new OrderModel();
                        newOrder.Code = tr.ChildNodes[2].InnerText;
                        newOrder.HQCompanyCode = tr.ChildNodes[3].InnerText;
                        newOrder.OrderTypeName = tr.ChildNodes[4].InnerText;
                        newOrder.OrderDate = DateTime.Parse(tr.ChildNodes[6].InnerText);
                        newOrder.Amount = decimal.Parse(tr.ChildNodes[10].InnerText);
                        //获得订单编号对应的控件id
                        var inputs = tr.ChildNodes[1].
                            Descendants("input").Where(ip => ip.Attributes["type"].Value == "checkbox").ToList();
                        if (inputs.Count > 0)
                        {
                            newOrder.CtrID = inputs[0].Attributes["name"].Value;
                        }
                        result.Add(newOrder);
                    }
                }
            }
            return result;
        }
    }
}
